package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.makerspace.domain.AreaDO;
import com.example.makerspace.domain.LanguageDO;
import com.example.makerspace.dto.Result;
import com.example.makerspace.dto.project.LanguageBaseDTO;
import com.example.makerspace.mapper.LanguageMapper;
import com.example.makerspace.service.LanguageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.example.makerspace.enums.ResultCodeEnum.LANGUAGE_EXIST;

@Service
public class LanguageServiceImpl implements LanguageService {

    @Autowired
    private LanguageMapper languageMapper;

    @Override
    public Result create(LanguageBaseDTO languageBaseDTO) {
        QueryWrapper<LanguageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("language_name", languageBaseDTO.getLanguageName());
        List<LanguageDO> languageDOS = languageMapper.selectList(queryWrapper);
        if (languageDOS.size() > 0) {
            return Result.success(LANGUAGE_EXIST);
        }
        LanguageDO languageDO = new LanguageDO();
        languageDO.setLanguageName(languageBaseDTO.getLanguageName());
        languageMapper.insert(languageDO);
        return Result.success(true);
    }

    @Override
    public Result query() {
        QueryWrapper<LanguageDO> queryWrapper = new QueryWrapper<>();
        List<LanguageDO> languageDOS = languageMapper.selectList(queryWrapper);
        return Result.success(languageDOS);
    }

    @Override
    public Result delete(Integer id) {
        languageMapper.deleteById(id);
        return Result.success(true);
    }

    @Override
    public Result update(LanguageBaseDTO languageBaseDTO) {
        QueryWrapper<LanguageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("language_name", languageBaseDTO.getLanguageName());
        List<LanguageDO> languageDOS = languageMapper.selectList(queryWrapper);
        if (languageDOS.size() > 0) {
            return Result.success(LANGUAGE_EXIST);
        }
        LanguageDO languageDO = new LanguageDO();
        languageDO.setLanguageName(languageBaseDTO.getLanguageName());
        languageDO.setId(languageBaseDTO.getId());
        languageDO.setModifyTime(new Date());
        languageMapper.updateById(languageDO);
        return Result.success(true);
    }
}
